<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div>
                <header style="height: 100%">
                    <div align="left">
	                    <table style="width: 100%">
	                   		<tr>
	                   			<td>
		                   			<form class="form-inline" onsubmit="return false">
										<div class="form-group">
											采购单号：
											<input id="id" type="text" class="form-control" >
                                            状态：
                                             <select class='form-control' class='form-control  input-sm'  type='text' name='status'   id='status' > 
                                                <option value="">全部</option>
                                                <option value="1">下单</option>
                                                <option value="2">向供应商下单</option>
                                                <option value="3">供应商发货</option>
                                                <option value="4">取消下单</option>
                                                <option value="5">部分收货</option>
                                                <option value="6">退货</option>
                                                <option value="7">全部收货</option>
                                                <option value="8">结算完结</option>
                                            </select>
											<button id="searchBt" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe615;</i>搜索</button>
										</div>
									</form>
	                   			</td>
	                   			<td align="right">
                            <button class="layui-btn layui-btn-sm" onclick="location.href='addPPurchOrders.html'">
                                      <i class="layui-icon">&#xe608;</i> 添加
                                    </button>

                             <button class="layui-btn layui-btn-sm" onclick="location.href='pPurchOrdersList.html'">
                                       返回
                                    </button>

	                   			</td>
	                   		</tr> 
	                    </table>
					</div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
									<th>采购单号</th>
									<th>采购日</th>
									<th>供应商</th>
									<th>采购方式</th>
									<th>状态</th>
									<th>预估商品总金额</th>
									<th>预估运费</th>
									<th>预估总金额</th>
									<th>退订金额</th>
									<th>实际总金额</th>
									<th>运输方式</th>
									<th>预计到货日</th>
									<th>创建时间</th>
									<th>更新时间</th>
									<th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript">
var pers = checkPermission();

var example;
function init(){
	example = 
    	$('#dt-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	"language": {
                "url": "/js/plugin/datatables/Chinese.lang"
            },
        	"ajax": {
        		"url" : "/pPurchOrderss",
        		"type":"get",
        		"data":function(d){
    				d.id = $("#id").val();
    			}
        	},
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
				{"data" : "id", "defaultContent" : ""},
				{"data" : "buydate", "defaultContent" : ""},
				{"data" : "supplierNo", "defaultContent" : ""},
				{"data" : "type", "defaultContent" : ""},
				{"data" : "status", "defaultContent" : ""},
				{"data" : "preGoodsAmount", "defaultContent" : ""},
				{"data" : "preExpenses", "defaultContent" : ""},
				{"data" : "preAmount", "defaultContent" : ""},
				{"data" : "cancelAmount", "defaultContent" : ""},
				{"data" : "amount", "defaultContent" : ""},
				{"data" : "transitType", "defaultContent" : ""},
				{"data" : "planReceiveDay", "defaultContent" : ""},
				{"data" : "createTime", "defaultContent" : ""},
				{"data" : "updateTime", "defaultContent" : ""},
				{ 
								"data": "", 
								"defaultContent": "",
								"orderable":false,
						        "render": function (data, type, row) {
                                   return buttonHtml(row,pers) ;
                  	            }
							},
                
            ],
           "order": [[ 0, "asc" ]]
        } );
}

layui.use('layer', function(){
    var layer = layui.layer;
});

function del(id){
	layer.confirm('确定要删除吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
    	$.ajax({
            type : 'delete',
            url : '/pPurchOrderss/'+id,
            success : function(data) {
                example.ajax.reload();
                layer.msg("删除成功");
            }
        });
        
        layer.close(1);
    });
}

function buttonHtml(row,pers){
       var id = row['id'];
       var href = "updatePPurchOrders.html?id=" + id;
       var edit = buttonEdit(href, "", pers);
       var del = buttonDel(id, "", pers);
       var Html = "";
       if(row['status']<=1 || row['status']==4 ){
           Html += edit + del;
       }
       if(row['status']<=1 ){
            href="pPurchOrderItemsListEdit.html?id="+id;
             Html += $("<button class='layui-btn layui-btn-xs' title='编辑采购单商品' onclick='window.location=\"" + href +"\"'>编辑采购单商品</button>").prop("outerHTML"); 
       }else{
            href="pPurchOrderItemsList.html?id="+id;
            Html +=  $("<button class='layui-btn layui-btn-xs' title='查看采购单商品' onclick='window.location=\"" + href +"\"'>查看采购单商品</button>").prop("outerHTML");            
       }
       //取消下单
       if(row['status']<=2){
            href="";
            Html += $("<button class='layui-btn layui-btn-xs' title='取消下单' onclick='confirmCancelOrder(\"" + id +"\")'>取消下单</button>").prop("outerHTML"); 
            
       }

       if(row['status']==2 || row['status']==3 || row['status']==5 || row['status']==6 ){
            href="pPurchOrderItemsListReceive.html?id="+id;
            Html += $("<button class='layui-btn layui-btn-xs' title='收货' onclick='window.location=\"" + href +"\"'>收货</button>").prop("outerHTML"); 
            
       }

       if(row['status']==7 || row['status']==6 ){
            href="";
            Html += $("<button class='layui-btn layui-btn-xs' title='结算完结' onclick='confirmCompleteOrder(\"" + id +"\")'>结算完结</button>").prop("outerHTML"); 
            layer.confirm('确认结算完结?', function(index){
                 layer.close(index);
            });
       }
       return Html;
}

function confirmCancelOrder(id){
    layer.confirm('确认取消下单?', function(index){

         layer.close(index);
    });
}

function confirmCompleteOrder(id){
    layer.confirm('确认结算完结?', function(index){

         layer.close(index);
    });
}

$("#searchBt").click(function(){
	example.ajax.reload();
});

init();
</script>
